package com.hengxinyongli.iam.web.controller;

import com.hengxinyongli.iam.dto.Subject;
import com.hengxinyongli.iam.entity.Resource;
import com.hengxinyongli.iam.service.ResourceService;
import com.hengxinyongli.iam.web.constant.Res;
import com.hengxinyongli.iam.web.constant.Url;
import com.hengxinyongli.iam.web.vo.Responses;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;

/**
 * @author magic
 * @date 2018/9/25/025 14:23
 * @version 1.0
 * Description PermitController
 */
@RestController
@Api(description = "用户权限相关api")
public class PermitController {

    @Autowired
    ResourceService resourceService;

    @GetMapping(Url.PERMITS)
    @ApiOperation(value = "获取所有权限列表",notes = "获取所有权限列表")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Resource.class)
    )
    public Responses permits(@RequestParam(required = false) @ApiParam(value = "权限PID") String pid,@ApiIgnore @RequestAttribute Subject subject){
        List<Resource> resources = resourceService.selectByType(Res.ALL,pid,subject);
        return Responses.success(resources);
    }

    @GetMapping(Url.PERMITS_ONE)
    @ApiOperation(value = "获取指定ID权限信息",notes = "获取指定ID权限信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Resource.class)
    )
    public Responses permitsOne(@PathVariable @ApiParam(value = "资源编号") String id){
        Resource resource = resourceService.selectById(id);
        return Responses.success(resource);
    }

    @PostMapping(Url.PERMITS)
    @ApiOperation(value = "创建权限信息",notes = "创建权限信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Resource.class)
    )
    public Responses permitsCreate(@RequestBody @Validated PermitRo permitRo){
        Resource resource = new Resource();
        BeanUtils.copyProperties(permitRo,resource);
        if(Objects.isNull(resource.getCode())){
            String code = Long.toHexString(System.currentTimeMillis());
            resource.setCode(code);
        }
        resourceService.resourceCreate(resource);
        return Responses.success(resource);
    }

    @PutMapping(Url.PERMITS_ONE)
    @ApiOperation(value = "修改权限信息",notes = "修改权限信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Resource.class)
    )
    public Responses permitsUpdate(@PathVariable @ApiParam(value = "资源编号") String id,@RequestBody @Validated PermitRo permitRo){
        Resource resource = new Resource();
        BeanUtils.copyProperties(permitRo,resource);
        resourceService.resourceUpdate(id,resource);
        return Responses.success(resource);
    }

    @DeleteMapping(Url.PERMITS_ONE)
    @ApiOperation(value = "删除权限信息",notes = "删除权限信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功")
    )
    public Responses permitsDelete(@PathVariable @ApiParam(value = "资源编号") String id){
        resourceService.resourceDelete(id);
        return Responses.success(id);
    }

    @ApiModel
    public static class PermitRo {

        @ApiModelProperty(value = "权限名称")
        @NotEmpty(message = "权限名称不能为空")
        private String name;
        @ApiModelProperty(value = "权限父编号")
        @NotEmpty(message = "权限父编号不能为空")
        private String parentId;
        @ApiModelProperty(value = "类型")
        @NotEmpty(message = "权限类型不能为空")
        private String type;
        @ApiModelProperty(value = "权限URL地址")
        private String url;
        @ApiModelProperty(value = "权限code")
        private String code;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getParentId() {
            return parentId;
        }

        public void setParentId(String parentId) {
            this.parentId = parentId;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }

}
